import { ActionContext } from 'vuex'
import { local_storage } from '@/utils/storage'
import { IStore } from '../types'

export type IAppState = {
  subPackagesRoot: string | null
}

const state = {
  subPackagesRoot: local_storage.get('LOCAL_ROOT_ROUTER_TYPE') || 'default'
}

const mutations = {
  SET_SUB_PACKAGES_ROOT: (state: IAppState, token: string) => {
    state.subPackagesRoot = token
  }
}

const actions = {
  // 设置分包类型
  async setRootRouter({ commit }: ActionContext<IAppState, IStore>, params: string) {
    return new Promise((resolve, reject) => {
      local_storage.set('LOCAL_ROOT_ROUTER_TYPE', params)
      commit('SET_SUB_PACKAGES_ROOT', params)
      resolve(params)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
